<?php 
/** 
 * 包含min函数的栈
 * 定义栈的数据结构，请在该类型中实现一个能够得到栈中所含最小元素的min函数（时间复杂度应为O（1））。
 * 思路：最小辅助栈，
 *       push    stack     minstcak
 *        5	       5          5
 *        6        6          5
 *        7        7          5
 *        4        4          4
 *        1        1          1
 *        8        8          1
 *     minstcak 中在stack每次push中都要压入，且只压入当前的最小值
 *     stack 每次pop是，同时minstcak也要minstack
 */
class MinStack
{
	private static $stack = [];
	private static $minStack = [];

	public static function mypush($node)
	{
	    array_push(static::$stack, $node);
	    if (count(static::$minStack) > 1) {
	    	$min = static::$minStack[count(static::$minStack) - 1];
		    if ($node < $min) {
				array_push(static::$minStack, $node);
		    } else {
		    	array_push(static::$minStack, $min);
		    }
	    } else {
	    	array_push(static::$minStack, $node);
	    }
	    
	}

	public static function mypop()
	{
        array_pop(static::$minStack);
	    return array_pop(static::$stack);
	}

	public static function mytop()
	{
	    if (count(static::$stack) > 0) {
	    	return static::$stack[count(static::$stack) - 1];
	    } 
	    return null;
	}

	public static function mymin()
	{
	    if (count(static::$minStack) > 0) {
	    	return static::$minStack[count(static::$minStack) - 1];
	    } 
	    return null;
	}
}
MinStack::mypush(5);
MinStack::mypush(2);
MinStack::mypush(3);
MinStack::mypush(1);
MinStack::mypush(10);
//echo MinStack::mypop();
echo MinStack::mytop();